Imports System.Threading

Public Class CDataQueue
    Private m_RWLock As CReadWriteLock = New CReadWriteLock()
    Private m_queue As Queue
    Private m_lock As System.Threading.AutoResetEvent


    Public Sub New()
        m_queue = New Queue()
        m_lock = New System.Threading.AutoResetEvent(False)

    End Sub

    Public Function WriteText(ByVal text As String) As Boolean
        Try
            m_RWLock.WriteLock(5000)
            m_queue.Enqueue(text)
            m_lock.Set()
            m_RWLock.ReleaseWriteLock()
        Catch ex As Exception
        End Try
    End Function

    Public Function ReadTextWait(ByVal timeout As Integer) As String
        Dim result As String = ""
        Dim flg As Boolean = True
        Dim tm As Integer = 0

        Do While flg
            Try
                m_RWLock.ReadLock(5000)
                If m_queue.Count > 0 Then
                    result = m_queue.Dequeue()
                    flg = False
                End If
                m_RWLock.ReleaseReadLock()
            Catch ex As Exception
            End Try
            Try
                m_lock.WaitOne(500, False)
                tm += 500
                If tm >= timeout Then
                    flg = False
                End If
            Catch ex As Exception
            End Try
        Loop
        Return result
    End Function
End Class
